﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using log4net;
using System.Globalization;

namespace Tireguit.Nmon.Parser.Statisitic
{
    public class PROCElement : NmonStatElement
    {
        protected new ILog _log = LogManager.GetLogger(typeof(PROCElement));
        protected IDataParameter _zzzz = null;
        protected IDataParameter _runnable = null;
        protected IDataParameter _swapin = null;
        protected IDataParameter _pswitch = null;
        protected IDataParameter _syscall = null;
        protected IDataParameter _read = null;
        protected IDataParameter _write = null;
        protected IDataParameter _fork = null;
        protected IDataParameter _exec = null;
        protected IDataParameter _rcvint = null;
        protected IDataParameter _xmtint = null;
        protected IDataParameter _sem = null;
        protected IDataParameter _msg = null;
        public string Zzzz { get; protected set; }
        public float Runnable { get; protected set; }
        public float Swapin { get; protected set; }
        public float Pswitch { get; protected set; }
        public float Syscall { get; protected set; }
        public float Read { get; protected set; }
        public float Write { get; protected set; }
        public float Fork { get; protected set; }
        public float Exec { get; protected set; }
        public float Rcvint { get; protected set; }
        public float Xmtint { get; protected set; }
        public float Sem { get; protected set; }
        public float Msg { get; protected set; }



        public PROCElement(IDbConnection connection)
        {
            Balise = BaliseType.PROC;
            ListCommand = new List<System.Data.IDbCommand>();
            Connection = connection;
            Linestring = string.Empty;
            _insertreq = "INSERT INTO " + NmonTables.TablesDictionnary.Instance["PROC"] + " ([zzzz],[Runnable],[Swapin],[pswitch],[syscall],[read],[write],[fork],[exec],[rcvint],[xmtint],[sem],[msg]) VALUES (@zzzz,@Runnable,@Swapin,@pswitch,@syscall,@read,@write,@fork,@exec,@rcvint,@xmtint,@sem,@msg)";
            Command = Connection.CreateCommand();
            Command.CommandText = _insertreq;
            ListCommand.Add(Command);
            _zzzz = Command.CreateParameter();
            _zzzz.ParameterName = "@zzzz";
            _zzzz.DbType = DbType.String;
            _runnable = Command.CreateParameter();
            _runnable.ParameterName = "@Runnable";
            _runnable.DbType = DbType.Decimal;
            _swapin = Command.CreateParameter();
            _swapin.ParameterName = "@Swapin";
            _swapin.DbType = DbType.Decimal;
            _pswitch = Command.CreateParameter();
            _pswitch.ParameterName = "@pswitch";
            _pswitch.DbType = DbType.Decimal;
            _syscall = Command.CreateParameter();
            _syscall.ParameterName = "@syscall";
            _syscall.DbType = DbType.Decimal;
            _read = Command.CreateParameter();
            _read.ParameterName = "@read";
            _read.DbType = DbType.Decimal;
            _write = Command.CreateParameter();
            _write.ParameterName = "@write";
            _write.DbType = DbType.Decimal;
            _fork = Command.CreateParameter();
            _fork.ParameterName = "@fork";
            _fork.DbType = DbType.Decimal;
            _exec = Command.CreateParameter();
            _exec.ParameterName = "@exec";
            _exec.DbType = DbType.Decimal;
            _rcvint = Command.CreateParameter();
            _rcvint.ParameterName = "@rcvint";
            _rcvint.DbType = DbType.Decimal;
            _xmtint = Command.CreateParameter();
            _xmtint.ParameterName = "@xmtint";
            _xmtint.DbType = DbType.Decimal;
            _sem = Command.CreateParameter();
            _sem.ParameterName = "@sem";
            _sem.DbType = DbType.Decimal;
            _msg = Command.CreateParameter();
            _msg.ParameterName = "@msg";
            _msg.DbType = DbType.Decimal;
        }
        public PROCElement() : this(null) { }
        public override List<IDbCommand> GetInsertReq()
        {
            try
            {
                _log.Debug("start GetInsertReq");


                if (Command == null)
                {
                    Command = Connection.CreateCommand();
                    Command.CommandText = _insertreq;
                    ListCommand.Add(Command);
                }
                Command.Parameters.Clear();
                _zzzz.Value = Zzzz;
                _runnable.Value = Runnable;
                _swapin.Value = Swapin;
                _pswitch.Value = Pswitch;
                _syscall.Value = Syscall;
                _read.Value = Read;
                _write.Value = Write;
                _fork.Value = Fork;
                _exec.Value = Exec;
                _rcvint.Value = Rcvint;
                _xmtint.Value = Xmtint;
                _sem.Value = Sem;
                _msg.Value = Msg;
                Command.Parameters.Add(_zzzz);
                Command.Parameters.Add(_runnable);
                Command.Parameters.Add(_swapin);
                Command.Parameters.Add(_pswitch);
                Command.Parameters.Add(_syscall);
                Command.Parameters.Add(_read);
                Command.Parameters.Add(_write);
                Command.Parameters.Add(_fork);
                Command.Parameters.Add(_exec);
                Command.Parameters.Add(_rcvint);

                Command.Parameters.Add(_xmtint);
                Command.Parameters.Add(_sem);
                Command.Parameters.Add(_msg);


                return ListCommand;

            }
            catch (System.Exception ex)
            {
                _log.Error("GetInsertReq", ex);
                return null;
            }
            finally
            {
                _log.Debug("stop GetInsertReq");
            }
        }
        protected override bool InternalParse(string line)
        {
            try
            {
                _log.Debug("start InternalParse");
                if (string.IsNullOrEmpty(line))
                {
                    return false;
                }

                // AAAElement aaaelem = new AAAElement();
                string[] elements = line.Split(separ);
#if DEBUG
                if (!elements[0].StartsWith("PROC"))
                {
                    _log.Error("Not PROC Line");
                    throw new ArgumentOutOfRangeException("start");
                }
#endif
                if (elements[1].StartsWith("Proc"))
                {
                    //Skip information header 
                    return false;
                }
                Zzzz = elements[1];
                float res = -1;
                if (float.TryParse(elements[2], NumberStyles.Float, CultureInfo.InvariantCulture, out res))
                {
                    Runnable = res;

                }
                if (float.TryParse(elements[3], NumberStyles.Float, CultureInfo.InvariantCulture, out res))
                {
                    Swapin = res;

                }
                if (float.TryParse(elements[4], NumberStyles.Float, CultureInfo.InvariantCulture, out res))
                {
                    Pswitch = res;

                }
                if (float.TryParse(elements[5], NumberStyles.Float, CultureInfo.InvariantCulture, out res))
                {
                    Syscall = res;

                }
                if (float.TryParse(elements[6], NumberStyles.Float, CultureInfo.InvariantCulture, out res))
                {
                    Read = res;

                }
                if (float.TryParse(elements[7], NumberStyles.Float, CultureInfo.InvariantCulture, out res))
                {
                    Write = res;

                }
                if (float.TryParse(elements[8], NumberStyles.Float, CultureInfo.InvariantCulture, out res))
                {
                    Fork = res;

                }
                if (float.TryParse(elements[9], NumberStyles.Float, CultureInfo.InvariantCulture, out res))
                {
                    Exec = res;

                }
                if (float.TryParse(elements[10], NumberStyles.Float, CultureInfo.InvariantCulture, out res))
                {
                    Rcvint = res;

                }
                if (float.TryParse(elements[11], NumberStyles.Float, CultureInfo.InvariantCulture, out res))
                {
                    Xmtint = res;

                }
                if (float.TryParse(elements[12], NumberStyles.Float, CultureInfo.InvariantCulture, out res))
                {
                    Sem = res;

                }
                if (float.TryParse(elements[13], NumberStyles.Float, CultureInfo.InvariantCulture, out res))
                {
                    Msg = res;

                }


                return true;
                //  return aaaelem;

            }
            catch (System.Exception ex)
            {
                _log.Error("InternalParse", ex);
                return false;
            }
            finally
            {
                _log.Debug("stop InternalParse");
            }
        }
    }
}
